local map = {}
local this = map
function this:new()
    local o = {}
    setmetatable(o,self)
    self.__index = self
    return o
end
function this:insert(k,v)
    self[k] = v
end
function this:remove(k)
    if nil ~= self[k] then
        self[k] = nil
    end
end
function this:getpair(k)
    local value = nil
    if nil ~= self[k] then
        value = self[k]
    end
    return value
end
function this:clear()
    for k,_ in pairs(self) do
        if nil ~= self[k] then
            self[k] = nil
        end
    end
end

-- 字符串拆分
local function lua_string_split(str, split_char)
 local sub_str_tab = {};
 while (true) do
 local pos = string.find(str, split_char);
 if (not pos) then
  local size_t = table.getn(sub_str_tab)
  table.insert(sub_str_tab,size_t+1,str);
  break;
 end

 local sub_str = string.sub(str, 1, pos - 1);
 local size_t = table.getn(sub_str_tab)
 table.insert(sub_str_tab,size_t+1,sub_str);
 local t = string.len(str);
 str = string.sub(str, pos + 1, t);
 end
 return sub_str_tab;
end

--判断是否存在元素
local function isintable(tb,value)
    for k,v in pairs(tb) do
        if v == value then
            return true
        end
    end
    return false
end

local function duplicateRemoval(table1)
    local table2={}
     for key,val in pairs(table1) do
         table2[val]=true
     end
    local table3={}
      for key,val in pairs(table2) do
         table.insert(table3,key)
      end
    return table3
end

-- 设置库存到缓存function
local function setInventoryCache (key, fieldValueTable)
    --执行更新的数据
    local updateMap = map:new()
    local keyArray = {}
    local wantSetValueArray = {}
    -- 将入参hashMap 转换为keyArray 和wantSetValueArray数组
    for k, v in pairs(fieldValueTable) do
        table.insert(keyArray, k)
        table.insert(wantSetValueArray, v)
    end
    -- 根据filed值查询redis的数据
    -- 去redis批量获取哈希表key中多个field键值 HMGET  key  field  [field ...]
    local redisQueryValueArray = redis.call('HMGET',key,unpack(keyArray))
    local setKeyValueArray = {}
    for i in pairs(redisQueryValueArray) do
       local key = keyArray[i]
       local redisQueryValue = redisQueryValueArray[i]
       local wantSetValue =  wantSetValueArray[i];
       if(redisQueryValue) then
            --判断版本号
            redis.log(redis.LOG_DEBUG,'想要设置的值'..tostring(wantSetValue))
            redis.log(redis.LOG_DEBUG,'redis中的值'..tostring(redisQueryValue))
            if(cjson.decode(redisQueryValue).version) then
                if(cjson.decode(redisQueryValue).version < cjson.decode(wantSetValue).version) then
                        --需要设置缓存
                         redis.log(redis.LOG_DEBUG,'数据过旧,需要设置缓存'..tostring(wantSetValue))
                         table.insert(setKeyValueArray, key)
                         table.insert(setKeyValueArray, wantSetValue)
                         updateMap:insert(key,wantSetValue)
                end
            else
                --需要设置缓存
                        redis.log(redis.LOG_DEBUG,'数据无版本,需要设置缓存'..tostring(wantSetValue))
                          table.insert(setKeyValueArray, key)
                          table.insert(setKeyValueArray, wantSetValue)
                          updateMap:insert(key,wantSetValue)
            end
        else
            --需要设置缓存
            redis.log(redis.LOG_DEBUG,'redis不存在,需要设置缓存'..tostring(wantSetValue))
            table.insert(setKeyValueArray, key)
            table.insert(setKeyValueArray, wantSetValue)
            updateMap:insert(key,wantSetValue)
        end
    end
    --存reids HMSET  key  field  value [field value ...] 	//在一个哈希表key中存储多个键值对
    if(table.getn(setKeyValueArray) > 0) then
        redis.call('HMSET',key,unpack(setKeyValueArray))
    end
    return updateMap
end

--有货仓库列表设置
local function setHasInventoryCache(updateMap,key)
    local updateRedisHasInventoryMap = map:new()
    local deleteRedisHasInventoryMap = map:new()
    --设置有货仓库列表
    --组装查询key
    local hasInventoryField = {}
    for k, v in pairs(updateMap) do
          local tab = lua_string_split(k, "-")
          local skuCode = tostring(tab[2])
          table.insert(hasInventoryField, skuCode)
          local hasInventoryValue = updateRedisHasInventoryMap:getpair(skuCode)
          local delInventoryValue = deleteRedisHasInventoryMap:getpair(skuCode)
          if nil == hasInventoryValue then
                hasInventoryValue = {}
                updateRedisHasInventoryMap:insert(skuCode,hasInventoryValue)
          end
          if nil == delInventoryValue then
                      delInventoryValue = {}
                      deleteRedisHasInventoryMap:insert(skuCode,delInventoryValue)
          end
          -- 判断库存是否大于0
          if(cjson.decode(v).availableNum > 0) then
              table.insert(hasInventoryValue, tab[1])
          else
              table.insert(delInventoryValue, tab[1])
          end


    end
    if(table.getn(hasInventoryField) > 0) then
        local redisQueryValueArray = redis.call('HMGET',key,unpack(hasInventoryField))
        for i in pairs(redisQueryValueArray) do
              local k =   hasInventoryField[i]
              local v =   redisQueryValueArray[i]
              redis.log(redis.LOG_DEBUG,'有货仓key的值'..tostring(k))
              redis.log(redis.LOG_DEBUG,'查询出来的有货仓value的值'..tostring(v))

              --取并集
              local hasInventoryValue = updateRedisHasInventoryMap:getpair(k)
              local delInventoryValue = deleteRedisHasInventoryMap:getpair(k)
              if(v) then
                    --字符串拆分
                     while (true) do
                        local pos = string.find(v, ",");
                        if (not pos) then
                              if(not isintable(delInventoryValue,v)) then
                                  if(not ('' == v)) then
                                      table.insert(hasInventoryValue, v)
                                  end
                              end
                              break;
                        end
                        local sub_str = string.sub(v, 1, pos - 1);
                        if(not isintable(delInventoryValue,sub_str)) then
                             if(not ('' == sub_str)) then
                                  table.insert(hasInventoryValue, sub_str)
                             end
                        end

                        local t = string.len(v);
                        v = string.sub(v, pos + 1, t);
                     end
                    -- 去重
                   hasInventoryValue = duplicateRemoval(hasInventoryValue)
                   updateRedisHasInventoryMap:insert(k,hasInventoryValue)
              end
        end

        local   setKeyValueArray = {}
        for k, v in pairs(updateRedisHasInventoryMap) do
             table.insert(setKeyValueArray, k)
             if(table.getn(v) > 0) then
                table.insert(setKeyValueArray, table.concat(v,","))
             else
                table.insert(setKeyValueArray, '')
             end

        end
        if(table.getn(setKeyValueArray) > 0) then
           redis.call('HMSET',key,unpack(setKeyValueArray))
        end

    end
end

-- 执行业务逻辑
local receive_arg_json = cjson.decode(ARGV[1])
--设置库存缓存
local updateMap = setInventoryCache(KEYS[1],receive_arg_json)

setHasInventoryCache(updateMap,KEYS[2])
return ''
